import 'package:flutter/material.dart';
import 'package:interview/theme/app_colors.dart';

class HomeCalendar extends StatefulWidget {
  const HomeCalendar({super.key});

  @override
  HomeCalendarState createState() => HomeCalendarState();
}

class HomeCalendarState extends State<HomeCalendar> {
  final List<String> monthArr = [
    'JAN',
    'FEB',
    'MAR',
    'APR',
    'MAY',
    'JUN',
    'JUL',
    'AUG',
    'SEP',
    'OCT',
    'NOV',
    'DEC'
  ];

  late final int month;
  late final int date;
  late final int day;
  late final int offsetDay;

  @override
  void initState() {
    super.initState();
    final DateTime now = DateTime.now();
    month = now.month - 1; // 转为数组索引 (0-11)
    date = now.day;

    final DateTime nextMonth = DateTime(now.year, now.month + 1, 1);
    final DateTime currentMonth = DateTime(now.year, now.month, 1);
    day = nextMonth.difference(currentMonth).inDays;

    offsetDay = currentMonth.weekday % 7; // 周日=0，周一=1...
  }

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
      decoration: BoxDecoration(
        color: theme.extension<AppColors>()!.contrastMix2,
        borderRadius: BorderRadius.circular(12), // 设置圆角
      ),
      child: Row(
        children: [
          CalendarHeader(
            month: monthArr[month],
            date: date.toString().padLeft(2, '0'),
            color: theme.scaffoldBackgroundColor,
          ),
          const SizedBox(width: 16),
          // 每日推荐
          const DailyRecommendation(),
          const SizedBox(width: 32),
          // 日历网格
          Expanded(
            child: CalendarGrid(
              offsetDay: offsetDay,
              day: day,
              currentDay: date,
            ),
          ),
        ],
      ),
    );
  }
}

// 顶部日期部分
class CalendarHeader extends StatelessWidget {
  final String month;
  final String date;
  final Color color;

  const CalendarHeader(
      {super.key,
      required this.month,
      required this.date,
      required this.color});

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 12 , vertical: 8),
      decoration: BoxDecoration(
        color: color,
        borderRadius: BorderRadius.circular(12),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.2),
            blurRadius: 4,
          ),
        ],
      ),
      child: Column(
        children: [
          Text(
            month,
            style: const TextStyle(color: Colors.orange, fontSize: 14),
          ),
          Text(
            date,
            style: const TextStyle(fontSize: 28, fontWeight: FontWeight.bold),
          ),
        ],
      ),
    );
  }
}

// 每日推荐部分
class DailyRecommendation extends StatelessWidget {
  const DailyRecommendation({super.key});
  @override
  Widget build(BuildContext context) {
    return const Expanded(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            '每日一题',
            style: TextStyle(fontSize: 10),
          ),
          Text(
            'Flutter中有哪些Weiget？',
            style: TextStyle(
              fontWeight: FontWeight.bold,
              fontSize: 12,
            ),
          ),
        ],
      ),
    );
  }
}

// 日历网格部分
class CalendarGrid extends StatelessWidget {
  final int offsetDay;
  final int day;
  final int currentDay;

  const CalendarGrid({
    super.key,
    required this.offsetDay,
    required this.day,
    required this.currentDay,
  });

  @override
  Widget build(BuildContext context) {
    return GridView.builder(
      physics: const NeverScrollableScrollPhysics(),
      shrinkWrap: true,
      gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 7, // 每行显示7天
        crossAxisSpacing: 10, // 网格水平间距
        mainAxisSpacing: 10, // 网格垂直间距
        childAspectRatio: 1, // 单元格宽高比
      ),
      itemCount: offsetDay + day,
      itemBuilder: (context, index) {
        if (index < offsetDay) {
          return const SizedBox.shrink(); // 空白单元格
        }
        final int currentDate = index - offsetDay + 1;
        return CalendarCell(
          date: currentDate,
          isToday: currentDate == currentDay,
        );
      },
    );
  }
}

// 日历单元格
class CalendarCell extends StatelessWidget {
  final int date;
  final bool isToday;

  const CalendarCell({
    super.key,
    required this.date,
    required this.isToday,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: isToday
            ? Colors.green.withOpacity(0.5)
            : Colors.grey.withOpacity(0.3),
        shape: BoxShape.circle, // 设置为圆形
      ),
      alignment: Alignment.center,
    );
  }
}
